//
//  PS_ChooseProductViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/11/29.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_ChooseProductViewController.h"
#import "PS_ProductTableViewCell.h"
#import "PS_ProductTopTableViewCell.h"
#import "PS_CDViewController.h"
#import "PS_FDViewController.h"
#import "PS_XDViewController.h"
#import "PS_XDTwoViewController.h"
#import "PS_XDThreeViewController.h"
#import "PS_XDFourViewController.h"
@interface PS_ChooseProductViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>{
    
    UILabel * markLabel;
    CGFloat webViewHeight;
    NSString * description;
    float tableHeight;
    NSArray * subProductListArr;
    NSDictionary * dataDic;//第一个cell内容
}
//@property(nonatomic,strong)UIScrollView * myScroll;
@property(nonatomic,strong)UITableView * myTable;
@property(nonatomic,strong)UIWebView *topwebView;
@property(nonatomic,strong)UILabel *topLabel;
@property(nonatomic,strong)UIView * headerBackView;
@property(nonatomic,assign)BOOL isObserving;

@end

@implementation PS_ChooseProductViewController

//- (UIScrollView *)myScroll{
//
//    if (_myScroll == nil) {
//
//        _myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight-SafeAreaBottomHeight)];
//        _myScroll.delegate = self;
//    }
//    return _myScroll;
//}
//- (UIView *)headerBackView{
//
//    if (_headerBackView == nil) {
//
//        _headerBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,150)];
//
//        markLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-40, 20)];
//        markLabel.textColor = [UIColor redColor];
//        markLabel.text = @"房屋贷款";
//        [_headerBackView addSubview:markLabel];
//
//        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(20, 50, SCREEN_WIDTH-40, 1)];
//        lineView.backgroundColor = [UIColor lightGrayColor];
//        [_headerBackView addSubview:lineView];
//
//        _topwebView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 51, SCREEN_WIDTH-20, 100)];
//        _topwebView.backgroundColor = [UIColor yellowColor];
//        _topwebView.delegate = self;
//        [_headerBackView addSubview:_topwebView];
//
//    }
//
//    return _headerBackView;
//
//}
- (UITableView *)myTable{
    
    if (_myTable == nil) {
        
        _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight-SafeAreaBottomHeight-10)];
        _myTable.delegate = self;
        _myTable.dataSource = self;
        _myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTable.scrollEnabled = YES;
        

    }
    return _myTable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"选择产品";

    [self getProductDetail];
    [self.view addSubview:self.myTable];
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return subProductListArr.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//    if (indexPath.row == 0) {
//
//        NSAttributedString * htmlStr = [[NSAttributedString alloc] initWithData:[description dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//
//         CGFloat height =[htmlStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
//
//        return height + 70;
//
//    }else{
//
//        NSDictionary * subProductDic = [subProductListArr objectAtIndex:indexPath.row];
//        NSString * descriptionStr = [subProductDic objectForKey:@"description"];
//        NSAttributedString * htmlStr = [[NSAttributedString alloc] initWithData:[descriptionStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//        CGFloat height =[htmlStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
//
//        return height + 90;
//    }
    
    self.myTable.rowHeight = UITableViewAutomaticDimension;
    self.myTable.estimatedRowHeight = 44;
    return self.myTable.rowHeight;

    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        

        PS_ProductTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_ProductTopTableViewCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PS_ProductTopTableViewCell" owner:self options:nil]firstObject];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSString *name = [dataDic objectForKey:@"name"];
        cell.nameLabel.text = name;
        
        NSString * descriptionStr = [dataDic objectForKey:@"description"];
        NSAttributedString * htmlStr = [[NSAttributedString alloc] initWithData:[descriptionStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        cell.contentLabel.attributedText = htmlStr;
        
        return cell;
        
    }else{
        NSDictionary * subProductDic = [subProductListArr objectAtIndex:indexPath.row-1];
        
        PS_ProductTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_ProductTableViewCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PS_ProductTableViewCell" owner:self options:nil]firstObject];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString * descriptionStr = [subProductDic objectForKey:@"description"];
        NSAttributedString * htmlStr = [[NSAttributedString alloc] initWithData:[descriptionStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        cell.contentLabel.attributedText = htmlStr;
        NSString * name = [subProductDic objectForKey:@"name"];
        cell.nameLabel.text = name;
        
        cell.submiButton.tag = indexPath.row - 1;
        
        [cell.submiButton addTarget:self action:@selector(pushNext:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }
    
}

- (void)pushNext:(UIButton *)sender{
    
    NSInteger index = sender.tag;
    NSInteger pID = [[dataDic objectForKey:@"id"] integerValue];
    NSDictionary * subProductDic = [subProductListArr objectAtIndex:index];
    NSString * subID = [subProductDic objectForKey:@"id"];
    NSString * name = [subProductDic objectForKey:@"name"];
    NSInteger type_id = [[subProductDic objectForKey:@"type_id"] integerValue];
        
    if (pID == 11) {
            PS_FDViewController * VC = [[PS_FDViewController alloc]initWithNibName:@"PS_FDViewController" bundle:nil];
            VC.subPID = subID;
            VC.proName = name;
            [self.navigationController pushViewController:VC animated:YES];
            
    }else if (pID==12){
            
            PS_CDViewController * VC = [[PS_CDViewController alloc]initWithNibName:@"PS_CDViewController" bundle:nil];
            VC.subPID = subID;
            VC.proName = name;
           [self.navigationController pushViewController:VC animated:YES];
            
    }else if(pID==14){
            //信贷模板对应： 1：u房贷    2：业主贷  3：寿险2.0  4：车主贷
            switch (type_id) {
                case 1:
                {
                    PS_XDThreeViewController * VC =[[PS_XDThreeViewController alloc]initWithNibName:@"PS_XDThreeViewController" bundle:nil];
                    VC.subId = subID;
                    VC.proName = name;
                    [self.navigationController pushViewController:VC animated:YES];
                }
                    break;
                case 2:
                {
                    PS_XDTwoViewController * VC =[[PS_XDTwoViewController alloc]initWithNibName:@"PS_XDTwoViewController" bundle:nil];
                    VC.subId = subID;
                    VC.proName = name;
                    [self.navigationController pushViewController:VC animated:YES];
                    
                }
                    break;
                case 3:
                {
                    PS_XDViewController * VC = [[PS_XDViewController alloc]initWithNibName:@"PS_XDViewController" bundle:nil];
                    VC.subId = subID;
                    VC.proName = name;
                    [self.navigationController pushViewController:VC animated:YES];
                }
                    break;
                case 4:
                {
                    PS_XDFourViewController * VC =[[PS_XDFourViewController alloc]initWithNibName:@"PS_XDFourViewController" bundle:nil];
                    VC.subId = subID;
                    VC.proName = name;
                    [self.navigationController pushViewController:VC animated:YES];
                }
                    break;
                    
                default:
                    break;
                    
                    
            }
        }
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    NSInteger pID = [[dataDic objectForKey:@"id"] integerValue];
//    NSDictionary * subProductDic = [subProductListArr objectAtIndex:indexPath.row-1];
//    NSString * subID = [subProductDic objectForKey:@"id"];
//    NSInteger type_id = [[subProductDic objectForKey:@"type_id"] integerValue];
//    if (indexPath.row>0) {
//
//        if (pID == 11) {
//            PS_FDViewController * VC = [[PS_FDViewController alloc]initWithNibName:@"PS_FDViewController" bundle:nil];
//            VC.subPID = subID;
//            [self.navigationController pushViewController:VC animated:YES];
//
//        }else if (pID==12){
//
//            PS_CDViewController * VC = [[PS_CDViewController alloc]initWithNibName:@"PS_CDViewController" bundle:nil];
//            [self.navigationController pushViewController:VC animated:YES];
//
//        }else if(pID==14){
//            //信贷模板对应： 1：u房贷    2：业主贷  3：寿险2.0  4：车主贷
//            switch (type_id) {
//                case 1:
//                {
//                    PS_XDThreeViewController * VC =[[PS_XDThreeViewController alloc]initWithNibName:@"PS_XDThreeViewController" bundle:nil];
//                    [self.navigationController pushViewController:VC animated:YES];
//                }
//                    break;
//                case 2:
//                {
//                    PS_XDTwoViewController * VC =[[PS_XDTwoViewController alloc]initWithNibName:@"PS_XDTwoViewController" bundle:nil];
//                    [self.navigationController pushViewController:VC animated:YES];
//
//                }
//                    break;
//                case 3:
//                {
//                    PS_XDViewController * VC = [[PS_XDViewController alloc]initWithNibName:@"PS_XDViewController" bundle:nil];
//                    [self.navigationController pushViewController:VC animated:YES];
//                }
//                    break;
//                case 4:
//                {
//                    PS_XDFourViewController * VC =[[PS_XDFourViewController alloc]initWithNibName:@"PS_XDFourViewController" bundle:nil];
//                    [self.navigationController pushViewController:VC animated:YES];
//                }
//                    break;
//
//                default:
//                    break;
//
//
//            }
//        }
//    }
//}

- (void)getProductDetail
{
    [Utils post:GETPRODUCT parameters:@{@"pid":_pId} success:^(NSInteger recode, id jsonObject) {
        

        self->dataDic = [jsonObject objectForKey:@"data"];
        self->subProductListArr = [jsonObject objectForKey:@"subProductList"];
        [self.myTable reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
